package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器广播其他角色使用技能消息
 */
public class WorldMap_OtherRoleUseSkillProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 13011; }

    public Integer AttackRoleId; //发起攻击角色Id
    public Integer SkillId; //技能编号
    public Integer SkillLevel; //技能等级
    public Float RolePosX; //角色坐标X
    public Float RolePosY; //角色坐标Y
    public Float RolePosZ; //角色坐标Z
    public Float RoleYAngle; //角色Y轴旋转
    public Integer BeAttackCount; //被攻击者数量
    public ArrayList<BeAttackItem> ItemList; //被攻击者

    /**
     * 被攻击者
     */
    public static class BeAttackItem
    {
        public Integer BeAttackRoleId; //被攻击者编号
        public Integer ReduceHp; //减少HP
        public byte IsCri; //是否暴击
    }

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteInt(AttackRoleId);
            dataStream.WriteInt(SkillId);
            dataStream.WriteInt(SkillLevel);
            dataStream.WriteFloat(RolePosX);
            dataStream.WriteFloat(RolePosY);
            dataStream.WriteFloat(RolePosZ);
            dataStream.WriteFloat(RoleYAngle);
            dataStream.WriteInt(BeAttackCount);
            for (int i = 0; i < BeAttackCount; i++) {
                dataStream.WriteInt(ItemList.get(i).BeAttackRoleId);
                dataStream.WriteInt(ItemList.get(i).ReduceHp);
                dataStream.WriteByte(ItemList.get(i).IsCri);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static WorldMap_OtherRoleUseSkillProto GetProto(byte[] buffer) {
        WorldMap_OtherRoleUseSkillProto proto = new WorldMap_OtherRoleUseSkillProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.AttackRoleId = dataStream.ReadInt();
            proto.SkillId = dataStream.ReadInt();
            proto.SkillLevel = dataStream.ReadInt();
            proto.RolePosX = dataStream.ReadFloat();
            proto.RolePosY = dataStream.ReadFloat();
            proto.RolePosZ = dataStream.ReadFloat();
            proto.RoleYAngle = dataStream.ReadFloat();
            proto.BeAttackCount = dataStream.ReadInt();
            proto.ItemList = new ArrayList<BeAttackItem>();
            for (int i = 0; i < proto.BeAttackCount; i++) {
                BeAttackItem _Item = new BeAttackItem();
                _Item.BeAttackRoleId = dataStream.ReadInt();
                _Item.ReduceHp = dataStream.ReadInt();
                _Item.IsCri = (byte)dataStream.ReadByte();
                proto.ItemList.add(_Item);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}